/**
* @program: src
*
* @description:群管理
*
* @author: Mr.chen
*
* @create: 2020-12-10 11:31
**/
package group
import (
	"ydd-im/service/api/services/wsservice/account"
	//"ydd-im/service/api/services/wsservice/client"
	"ydd-im/service/api/services/wsservice/group"
	"ydd-im/service/api/tools/bind"
	"ydd-im/service/api/tools/response"
	"ydd-im/service/api/tools/util"
	"github.com/gin-gonic/gin"
	"strings"
)

type Controller struct {
}
type inputData struct {
	Id   string `json:"id" form:"id" ` // 群Id,客户自定义的id
	Name string `json:"name" form:"name" binding:"required" ` // 群名称
	Members string `json:"members" form:"members" binding:"required" ` //  群成员列表，逗号隔开,对应用户表的userkey
}

type inputJoingroupData struct {
	Members string  `json:"members" form:"members" binding:"required" ` //  群成员列表 逗号隔开
	GroupId string `json:"groupid" form:"groupid" `

}

/**
 * 创建群
 * @access  public
 * @param   string     id  群id
 * @param   string     members  用户，多个以逗号隔开
 * @param   string     Name  群名称
 * @param   string     appkey  appkey
 * @return  mix
 */
func (r *Controller) Creategroup(c *gin.Context) {
	var inputData inputData
	_, err := bind.Bind(&inputData, c)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message:"参数有误，请重试",
		})
		return
	}
	if len(inputData.Id) == 0 {
		inputData.Id = util.GenUUID();
	}
	// 获取用户信息
	userInfo, err := account.GetMiddleUserInfo(c)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	members :=strings.Split(inputData.Members,",")
	if userInfo.UserKey != "" {
		members = append(members,userInfo.UserKey)
	}

	members = util.RemoveRepByMapString(members)

	// 获取所需的消息列表
	err = group.AddGroupIsCluster(userInfo.Id,userInfo.Sid,inputData.Id,inputData.Name,members)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	response.ResponseJsonOk(c, response.Response{
		Code:    response.SuccessCode,
		Message: "success",
		Data:map[string]string{
			"groupid":inputData.Id,
		},
	})
	return
}

/**
 * 绑定用户入群
 * @access  public
 * @param   string     members  用户，多个以逗号隔开
 * @param   string     groupid  群id
 * @param   string     appkey  appkey
 * @return  mix
 */

func (r *Controller) Joingroup(c *gin.Context) {
	var inputData inputJoingroupData
	_, err := bind.Bind(&inputData, c)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	// 获取token查询的用户信息
	userInfo,err := account.GetMiddleUserInfo(c)
	if  err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	members :=strings.Split(inputData.Members,",")
	err = group.AddGroupContactIsCluster(userInfo.Id,userInfo.Sid,inputData.GroupId,members)
	// 判断添加用户是否在群内
	//ok,err := group.GetGroupUserInfo(inputData.ToUserId,inputData.GroupId)
	//if err != nil {
	//	response.ResponseJsonFail(c, response.Response{
	//		Code:  response.ErrorCode,
	//		Message: err.Error(),
	//	})
	//	return
	//}
	//if !ok { // 用户不在群内，添加群
	//	err = group.AddGroupContact(inputData.ToUserId,inputData.GroupId)
	//	if err != nil {
	//		response.ResponseJsonFail(c, response.Response{
	//			Code:  response.ErrorCode,
	//			Message: err.Error(),
	//		})
	//		return
	//	}
	//}
	//client.AddUserToGroup(inputData.GroupId, inputData.ToUserId)
	response.ResponseJsonOk(c, response.Response{
		Code:  response.SuccessCode,
		Message: "success",

	})
	return
}